﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_wcupdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int cid = InitFromUrl("cid", 0);
            if (cid == 0)
            {
                Response.Redirect("admin_works.aspx");
            }
            if (!IsPostBack)
            {
                BindDate(cid);
            }
            imgPicUrl.ImageUrl = hfPicUrl.Value;
        }
        private void BindDate(int cid)
        {
            BindWorksCategory(cid);
            BindNotice();
        }
        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }
        private void BindWorksCategory(int cid)
        {
            WorksCategoryInfo wc = WorksCategoryInfoLogic.GetOneWorksInfo(cid);
            txbTitle.Text = wc.CategoryName;
            txbIntroOne.Text = wc.IntroOne;
            txbIntroTwo.Text = wc.IntroTwo;
            txbIntroThree.Text = wc.IntroThree;
            hfPicUrl.Value = wc.CategoryPicURL;
        }
        private int InitFromUrl(string url,int first)
        {
            int rtn = first;
            try{
                rtn = Int32.Parse(Request.QueryString[url].ToString());
            }catch{
            }
            return rtn;
        }
        private WorksCategoryInfo createWorksCategory()
        {
            WorksCategoryInfo wc = new WorksCategoryInfo();
            wc.CategoryName = txbTitle.Text.Replace("'", "''");
            wc.IntroOne = txbIntroOne.Text.Replace("'", "''");
            wc.IntroTwo = txbIntroTwo.Text.Replace("'", "''");
            wc.IntroThree = txbIntroThree.Text.Replace("'", "''");
            wc.CategoryPicURL = hfPicUrl.Value;
            wc.HasNWork = 0;
            return wc;
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            WorksCategoryInfo wc = createWorksCategory();
            wc.CategoryID = InitFromUrl("cid",0);
            if (wc.CategoryID == 0)
            {
                return;
            }
            int isSuccess = WorksCategoryInfoLogic.UpdateWorksCategoryInfo(wc);
            if(isSuccess != 1)
            {
                ChangeStatus(false);
            }
            else
            {
                ChangeStatus(true);
            }
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();", true);
        }
    }
}
